#! python3
# 处理 JSON 数据

import json

if __name__ == '__main__':
    stringOfJsonData = '{"name": "Zophie", "isCat": true, "miceCaught": 0, "felineIQ": null}'

    # JSON字符串转换为Python值：load string
    jsonDict = json.loads(stringOfJsonData)
    print(type(jsonDict))
    print(jsonDict.get('name'))

    # 将一个Python值转换成JSON字符串：dump string
    jsonStr = json.dumps(jsonDict)
    print(type(jsonStr))
    print(jsonStr)

    # 写入 JSON 数据
    with open('data.json', 'w') as f:
        json.dump(jsonStr, f)

    # 读取数据
    with open('data.json', 'r') as f:
        data = json.load(f)
